/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui.slidebox;

import java.awt.BorderLayout;

import javax.swing.Action;
import javax.swing.GroupLayout;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;

import com.pearreview.ui.FormLabel;
import com.pearreview.application.Messages;
import com.pearreview.util.review.Attendee;

public class AvailableAttendeeSlideBox extends SlideBox<Attendee> {

	private static final long serialVersionUID = -6551529187405901405L;

	private Long preparationTime;

	public AvailableAttendeeSlideBox(Attendee attendee, Action delete, 
			Action edit) {
		super(attendee, delete, edit);
		checkLabel.setVisible(true);
		checkLabel.setToolTipText(Messages.getString(
				"PearReview.Scribe.attendees.present"));
		slidedHeight = 125;
		setDeleteEnabled(false);
	}
	
	public void setAvailable(boolean available) {
		this.checkLabel.setChecked(available);
		this.updateBox();
	}
	
	public boolean isAvailable() {
		return this.checkLabel.isChecked();
	}
	
	public void setPreperationTime(Long preperaationTime) {
		this.preparationTime = preperaationTime;
	}
	
	public Long getPreparationTime() {
		return preparationTime;
	}
	
	@Override
	protected String getCaption() {
    	return Messages.getString("PearReview.Moderator.attendees.name") + 
    			": " + element.getName();
    }
	
	@Override
	public void setDeleteEnabled(boolean enabled) {
		//should never be enabled
		this.deleteBtn.setEnabled(false);
		this.deleteBtn.setVisible(false);
	}
	
    
	@Override
    protected void initContent(JPanel containerBottom) {

		FormLabel attendeeNameLabel = new FormLabel(
				Messages.getString("PearReview.Moderator.attendees.name"));
		FormLabel attendeeName = new FormLabel(element.getName());
		FormLabel attendeeContactLabel = new FormLabel(
				Messages.getString("PearReview.Moderator.attendees.contact"));
		FormLabel attendeeContact = new FormLabel(element.getContact());
		FormLabel attendeeRoleLabel = new FormLabel(
				Messages.getString("PearReview.Moderator.attendees.role"));
		FormLabel attendeeRole = new FormLabel();
		FormLabel prepTimeLabel = new FormLabel(Messages.getString(
				"PearReview.Scribe.attendees.preparation_time_short"));
		FormLabel prepTime;
		
		if (this.checkLabel.isChecked() && preparationTime != null) {			
			prepTime = new FormLabel(millisToString(preparationTime));
		} else {
			prepTime = new FormLabel();
		}
		
		switch (element.getRole()) {
		case Moderator:
			attendeeRole.setText(
					Messages.getString("PearReview.review.modarator"));
			break;
		
		case Scribe:
			attendeeRole.setText(
					Messages.getString("PearReview.review.scribe"));
			break;
			
		case Reviewer:
			attendeeRole.setText(
					Messages.getString("PearReview.review.reviewer"));
			break;
			
		case Author:
			attendeeRole.setText(
					Messages.getString("PearReview.review.author"));
			break;
			
		case Customer:
			attendeeRole.setText(
					Messages.getString("PearReview.review.customer"));
			break;
		}
  
		
		containerBottom.setLayout(new BorderLayout());

		GroupLayout layout = new GroupLayout(containerBottom);
		
		containerBottom.setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(layout.createParallelGroup()
						.addGroup(layout.createSequentialGroup()
							.addGroup(layout.createParallelGroup()
								.addGroup(GroupLayout.Alignment.TRAILING, 
										layout.createSequentialGroup()
									.addGroup(layout.createParallelGroup()
										.addComponent(attendeeNameLabel, 
												GroupLayout.Alignment.LEADING, 
												GroupLayout.DEFAULT_SIZE, 
												50, 
												GroupLayout.PREFERRED_SIZE)
										.addComponent(attendeeContactLabel,
												GroupLayout.Alignment.LEADING, 
												GroupLayout.DEFAULT_SIZE, 
												50, 
												GroupLayout.PREFERRED_SIZE)
										.addComponent(attendeeRoleLabel,
												GroupLayout.Alignment.LEADING, 
												GroupLayout.DEFAULT_SIZE, 
												50, 
												GroupLayout.PREFERRED_SIZE)
										.addComponent(prepTimeLabel,
												GroupLayout.Alignment.LEADING, 
												GroupLayout.DEFAULT_SIZE, 
												50, 
												GroupLayout.PREFERRED_SIZE))
									.addGap(57, 57, 57)
									.addGroup(layout.createParallelGroup(
												GroupLayout.Alignment.TRAILING)
										.addComponent(prepTime, 
												GroupLayout.Alignment.LEADING,
												10, 
												GroupLayout.PREFERRED_SIZE, 
												GroupLayout.PREFERRED_SIZE)
										.addComponent(attendeeRole, 
												GroupLayout.Alignment.LEADING, 
												10, 
												GroupLayout.PREFERRED_SIZE, 
												GroupLayout.PREFERRED_SIZE)
										.addComponent(attendeeContact, 
												GroupLayout.Alignment.LEADING, 
												10, 
												GroupLayout.PREFERRED_SIZE, 
												GroupLayout.PREFERRED_SIZE)
										.addComponent(attendeeName, 
												GroupLayout.Alignment.LEADING, 
												10, 
												GroupLayout.PREFERRED_SIZE, 
												GroupLayout.PREFERRED_SIZE))
									.addGap(10, 10, 10))))))
								
		);
		layout.setVerticalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(layout.createParallelGroup(GroupLayout
							.Alignment.BASELINE)
						.addComponent(attendeeNameLabel)
						.addComponent(attendeeName, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					.addGroup(layout.createParallelGroup()
						.addComponent(attendeeContactLabel)
						.addComponent(attendeeContact, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					.addGroup(layout.createParallelGroup()
						.addComponent(attendeeRoleLabel)
						.addComponent(attendeeRole, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					.addGroup(layout.createParallelGroup()
						.addComponent(prepTimeLabel)
						.addComponent(prepTime, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED))
		);
    }
	
	public String millisToString(long millisTime) {
		
		millisTime /= 1000;
		int seconds = (int)(millisTime%60); millisTime /= 60;
		int minutes = (int)(millisTime%60); millisTime /= 60;
		int hours = (int)(millisTime%60);
		
		String secondsStr = Integer.toString(seconds); 
		if (seconds < 10)
			secondsStr = "0" + secondsStr;
		
		String minutesStr = Integer.toString(minutes); 
		if (minutes < 10)
			minutesStr = "0" + minutesStr;
		
		String hoursStr = Integer.toString(hours); 
		if (hours < 10)
			hoursStr = "0" + hoursStr;
			
		return hoursStr + ":" + minutesStr;
	}
	
}